import utils from './CommonUtils'
import {Loading} from 'element-ui';

export default {
  /**
   * 将value值 序列化成字符窜存储，在恢复表单的值的时候，将其表单的默认对象还原回去
   * @param that 页面对象
   * @param fromKey  data对象的 form对象key
   * @param value  值
   */
  initFormStr(that, fromKey, value) {
    that[fromKey] = JSON.stringify(value);
  }
  , resetForm(that, key, value) {
    that[key] = JSON.parse(value);
  }
  /**
   *
   * @param that 当前页面的that
   * @param formKey 要赋值的formKey data 里面的对象
   * @param value 值对象
   * @param skipKey 哪些key不需要赋值
   * @param skipKey 哪些key不需要赋值
   * @param defaultValue value 为空的时候，还原默认值
   */
  , setDataToForm(that, formKey, value, skipKey, defaultValue) {
    skipKey = utils.checkIsEmpty(skipKey) ? [] : skipKey;
    if (value != null) {
      for (const key in value) {
        if (value.hasOwnProperty(key)) {
          const element = value[key];
          if (skipKey.indexOf(key) >= 0) {
            continue;
          }
          if (that[formKey].hasOwnProperty(key)) {
            that[formKey][key] = element;
          }
        }
      }
    } else {
      that[formKey] = defaultValue
    }
  }

  /**
   * post请求
   * @param that 页面
   * @param url url
   * @param data 参数对象
   * @param callback 回调
   * @param reloadTable 是否刷新表格，需要在data的页面 有 reloadTable 函数
   */
  , initThis(that) {

  }
  , post(that, url, data, reloadTable, callback) {
    let loading = Loading.service({});
    that.$postJson(url, data).then(res => {
      this.reloadTableAndCallBack(that, reloadTable, res, callback, loading)
    })
  }
  , put(that, url, data, reloadTable, callback) {
    let loading = Loading.service({});
    that.$put(url, data).then(res => {
      this.reloadTableAndCallBack(that, reloadTable, res, callback, loading)
    })
  }
  , delete(that, url, reloadTable, tip, callback) {
    let defaultTip = "此操作将永久删除已选数据, 是否继续?"
    if (utils.checkIsEmpty(tip)) {
      tip = defaultTip;
    }

    that.$confirm(tip, "温馨提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }).then(() => {
      let loading = Loading.service({});
      that.$del(url, {}).then(res => {
        this.reloadTableAndCallBack(that, reloadTable, res, callback, loading)
      })
    })
  }
  , reloadTableAndCallBack(that, reloadTable, res, callback, loading) {
    loading.close();
    if (res.R) {
      that.$success(res.msg);
      if (reloadTable) {
        that.reloadTable();
      }
      if (!utils.checkIsEmpty(callback)) {
        callback(res);
      }
    }
  }
}

function a(callback) {
  callback("nihao")
}

let b = function (res) {
  debugger
}

// a(b)
